package mysql_9_18;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class ConnectDao {
	private static Properties configFile = new Properties();
	static {
		try {
			configFile.load(ConnectDao.class.getClassLoader().getResourceAsStream("Config.properties"));
			Class.forName(configFile.getProperty("driver"));
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	public Connection getConnection() {
		try {
			return DriverManager.getConnection(configFile.getProperty("url"), configFile.getProperty("name"),
					configFile.getProperty("pw"));
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public void closeAll(Connection con, Statement st, ResultSet rs) {
		close(rs);
		close(st);
		close(con);
	}

	public void close(ResultSet rs) {
		try {
			if (rs != null)
				rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public void close(Statement st) {
		try {
			if (st != null) {
				st.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public void close(Connection con) {
		try {
			if (con != null) {
				con.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
}
